package com.delamer.facile.exe;

import com.delamer.facile.db.DBUtilSwitch;
import com.delamer.facile.obj.ObjectSwitch;
import com.delamer.utils.MsgUtil;
import com.delamer.utils.PermitUtil;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class ExecutorSwitch {
    public void help(CommandSender sender, String[] args) throws Exception {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(Bukkit.getPluginManager().getPlugin("DelamerFacile").getResource("switch.help"), "UTF-8"));
        List<String> results = new ArrayList<String>();
        String line = null;
        while ((line = bufferedReader.readLine()) != null)
            results.add(line);
        sender.sendMessage(results.toArray(new String[]{}));
    }

    public void keep(CommandSender sender, String[] args) throws Exception {
        if (args.length == 1 && !(sender instanceof Player)) {
            sender.sendMessage(MsgUtil.err("通过控制台使用该指令时，目标玩家不能为空"));
            return;
        }
        if(!PermitUtil.hasPermit(sender.getName(),"switch.keep")){
            sender.sendMessage(MsgUtil.err("未取得授权的玩家无法使用该指令"));
            return;
        }
        Player target = args.length > 1 ? Bukkit.getPlayerExact(args[1]) : (Player) sender;
        if (target == null) {
            sender.sendMessage(MsgUtil.err("目标玩家不存在"));
            return;
        }
        ObjectSwitch object = DBUtilSwitch.getSwitch(new ObjectSwitch() {{
            setUserName(target.getName());
            setSwitchName("keep");
        }});
        if (object == null) {
            DBUtilSwitch.addSwitch(new ObjectSwitch() {{
                setUserName(target.getName());
                setSwitchName("keep");
                setSwitchState(1);
            }});
            sender.sendMessage(MsgUtil.msg("玩家 §f" + target.getName() + " §e当前的死亡掉落保护状态为 §a开启"));
        } else {
            object.setSwitchState(object.getSwitchState() * -1 + 1);
            DBUtilSwitch.setSwitch(object);
            sender.sendMessage(MsgUtil.msg("玩家 §f" + target.getName() + " §e当前的死亡掉落保护状态为 " + ((object.getSwitchState()) > 0 ? "§a开启" : "§c关闭")));
        }
    }

    public void color(CommandSender sender, String[] args) throws Exception {
        if (args.length == 1 && !(sender instanceof Player)) {
            sender.sendMessage(MsgUtil.err("通过控制台使用该指令时，目标玩家不能为空"));
            return;
        }
        if(!PermitUtil.hasPermit(sender.getName(),"switch.color")){
            sender.sendMessage(MsgUtil.err("未取得授权的玩家无法使用该指令"));
            return;
        }
        Player target = args.length > 1 ? Bukkit.getPlayerExact(args[1]) : (Player) sender;
        if (target == null) {
            sender.sendMessage(MsgUtil.err("目标玩家不存在"));
            return;
        }
        ObjectSwitch object = DBUtilSwitch.getSwitch(new ObjectSwitch() {{
            setUserName(target.getName());
            setSwitchName("color");
        }});
        if (object == null) {
            DBUtilSwitch.addSwitch(new ObjectSwitch() {{
                setUserName(target.getName());
                setSwitchName("color");
                setSwitchState(1);
            }});
            sender.sendMessage(MsgUtil.msg("玩家 §f" + target.getName() + " §e当前的颜色代码状态为 §a开启"));
        } else {
            object.setSwitchState(object.getSwitchState() * -1 + 1);
            DBUtilSwitch.setSwitch(object);
            sender.sendMessage(MsgUtil.msg("玩家 §f" + target.getName() + " §e当前的颜色代码状态为 " + ((object.getSwitchState()) > 0 ? "§a开启" : "§c关闭")));
        }
    }

}
